package com.yuma.cloud.order.service.impl;

import com.yuma.cloud.order.entity.Order;
import com.yuma.cloud.order.feign.UserFeignClient;
import com.yuma.cloud.user.entity.User;
import com.yuma.cloud.order.mapper.OrderMapper;
import com.yuma.cloud.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class OrderServiceImpl implements OrderService {

    int loopCount = 0;

    @Autowired
    private OrderMapper orderMapper ;

    @Autowired   // 远程调用
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public Order findOrderByOrderId(Long orderId) {
        Order order = orderMapper.findOrderByOrderId(orderId);
        Long userId = order.getUserId();
        User user = userFeignClient.findUserByUserId(userId);
        order.setUser(user);
        return order;
    }

   /* @Override
    public Order findOrderByOrderId(Long orderId) {
        Order order = orderMapper.findOrderByOrderId(orderId);
        Long userId = order.getUserId();
        // http://localhost:10100/api/user/findUserByUserId/1
        // 修改url把IP+PORT -> 服务名称
        User user = restTemplate
                .getForObject("http://service-user/api/user/findUserByUserId/" + userId, User.class);

        order.setUser(user);
        return order;
    }*/

    // 使用服务发现调用实例
   /* @Override
    public Order findOrderByOrderId(Long orderId) {
        Order order = orderMapper.findOrderByOrderId(orderId);
        Long userId = order.getUserId();
        // http://localhost:10100/api/user/findUserByUserId/1
        ServiceInstance serviceInstance = getServiceInstance2("service-user");
        String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();

        User user = restTemplate
                .getForObject("http://"+ url + "/api/user/findUserByUserId/" + userId, User.class);

        order.setUser(user);
        return order;
    }*/

     // 自己实现一个随机的负载均衡
    private ServiceInstance getServiceInstance(String serviceName) {
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        int size = instances.size();
        int index = ThreadLocalRandom.current().nextInt(size);
        ServiceInstance serviceInstance = instances.get(index); // 0~2
        return serviceInstance;
    }

    // 实现轮训算法
    private ServiceInstance getServiceInstance2(String serviceName) {
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        int size = instances.size();
        ServiceInstance serviceInstance = instances.get(loopCount++ % size);
        return serviceInstance;
    }

   /*
    @Override
    public Order findOrderByOrderId(Long orderId) {
        Order order = orderMapper.findOrderByOrderId(orderId);
        Long userId = order.getUserId();
        // http://localhost:10100/api/user/findUserByUserId/1
        User user = restTemplate
             .getForObject("http://localhost:10100/api/user/findUserByUserId/" + userId, User.class);

        order.setUser(user);
        return order;
    }*/
}